//
//  ViewController.m
//  04-nsoperationqueue的最大并发数
//
//  Created by J on 16/2/24.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import "CustomOperation.h"

@interface ViewController ()
@property(nonatomic,strong)NSOperationQueue * queue ;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    self.queue = queue;
//        queue.maxConcurrentOperationCount = 1;//就变成了串形队列了 >1就是并发队列 ==1就是串形队列
    
    //     NSOperationQueueDefaultMaxConcurrentOperationCount = -1;//默认不做限制 根据cpu情况
    
//    queue.maxConcurrentOperationCount = 2;//并发队列
//
//    [queue addOperationWithBlock:^{
//        NSLog(@"1%@",[NSThread currentThread]);
//        sleep(1);
//        
//    }];
//    [queue addOperationWithBlock:^{
//        NSLog(@"2%@",[NSThread currentThread]);
//        for (int i = 0; i<10000; i++) {
//            NSLog(@"%zd",i);//就算self.queue.suspended=YES 当前任务仍然会继续执行 直到次任务完毕才会暂定后面的任务 (无法中途暂停)
//        }
//        sleep(2);
//        
//    }];
//    [queue addOperationWithBlock:^{
//        NSLog(@"3%@",[NSThread currentThread]);
//        sleep(2);
//
//        
//    }];
//    
//    [queue addOperationWithBlock:^{
//        NSLog(@"4%@",[NSThread currentThread]);
//        sleep(2);
//
//    }];
//    
    
    [queue addOperation:[[CustomOperation alloc]init]];



}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    self.queue.suspended = !self.queue.suspended;//点一下就让self.queue暂停 没有从内存中销毁
    [self.queue cancelAllOperations];//(无法中途取消 它仍然会等当前操作执行完毕才会取消后面的所有操作)  相当于调用了所有operation的cancel方法//This method calls the cancel method on all operations currently in the queue.

 }

- (void)operationWithBlock{
    
    //创建操作队列
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//
    NSBlockOperation* blockOp =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
        
    }];
    
    [queue addOperation:blockOp];
    
    [queue addOperationWithBlock:^{//不创建操作Operation  直接添加block
        
        NSLog(@"%@",[NSThread currentThread]);
    }];
    
    
    NSUInteger count = [queue operationCount];//当前并发操作的任务数
//    NSLog(@"%zd",count);
    
}



@end
